package DAO;

import hibernate.HibernateUtil;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import clases.Mascota;




public class MascotaDAO implements IMascotaDAO{
	
	SessionFactory factory;
	
	public MascotaDAO(){
		
		factory= HibernateUtil.getSessionFactory();
	}
	
	@Override
	public Mascota findById(Integer id) {
		Session session= factory.openSession();
		Mascota mas= (Mascota)session.get(Mascota.class, id);
		session.close();
		return mas;
	}

	@Override
	public void saveOrUpdate(Mascota mas) {
		
		Session session=factory.openSession();
		Transaction tx=session.beginTransaction();
		session.saveOrUpdate(mas);
		tx.commit();
		session.close();
	}
	
	@Override
	public void delete(Mascota mas) {
		Session session=factory.openSession();
		Transaction tx=session.beginTransaction();
		session.delete(mas);
		tx.commit();
		session.close();
		
	}
	
	public Mascota findMas(int id){
		
		Session session= factory.openSession();
		Query q= session.createQuery("from Mascota m where m.id_mascota="+id);
		Mascota tmp=(Mascota)q.uniqueResult();
		return tmp;
	}

}
